#pragma once

#include <string>
#include <vector>
#include <string>
#include <string.h>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <functional>
#include "Log.hpp"

using namespace LogModule;

using func_t = std::function<std::string(const std::string&)>; // 定义回调函数类型

class UdpServer
{
public:
    // 构造函数
    UdpServer(/* const std::string &ip, */ uint16_t port, func_t func)
        : _sockfd(-1),
          _port(port),
          //_ip(ip),
          _isrunning(false),
          _func(func)
    {
    }

    // 服务器初始化
    void init()
    {
        // 创建套接字
        _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd < 0)
        {
            LOG(LogLevel::FATAL) << "create socket failed";
            exit(1);
        }
        LOG(LogLevel::INFO) << "create socket success, sockfd = " << _sockfd;

        //填充socket相关信息
        struct sockaddr_in local;
        bzero(&local, sizeof(local)); //清零

        local.sin_family = AF_INET;                   //协议族  
        local.sin_port = htons(_port);                //端口号
        //local.sin_addr.s_addr = inet_addr(_ip.c_str());//IP地址转化, 由字符串转化为网络字节序的二进制
        local.sin_addr.s_addr = INADDR_ANY; //监听所有网卡地址

        //绑定本地地址
        if(bind(_sockfd, (struct sockaddr*)&local, sizeof(local)) < 0)
        {
            LOG(LogLevel::FATAL) << "bind error";
            exit(2);
        }
        LOG(LogLevel::INFO) << "bind success, _sockfd = " << _sockfd; 
    }

    // 服务器启动
    void start()
    {
        _isrunning = true;

        while (_isrunning)
        {
            char buffer[1024];

            // 接收数据套接字地址
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int ret = recvfrom(_sockfd, buffer, sizeof(buffer) - 1, 0, (struct sockaddr*)&peer, &len);
            if(ret > 0)
            {
                buffer[ret] = 0; //字符串结束符
                LOG(LogLevel::DEBUG) << "received buffer: " << buffer; // 接受到的数据
                
                // 处理数据, 调用回调函数
                std::string result = _func(buffer); // 调用回调函数, 获取处理结果result

                // 接受到的数据来自谁
                int peer_port = ntohs(peer.sin_port); // 端口号
                std::string peer_ip = inet_ntoa(peer.sin_addr); // IP地址, 将网络格式转换为点分十进制字符串格式


                /* std::string echo_string = "server echo@ ";
                echo_string += buffer; */
                ssize_t send_ret = sendto(_sockfd, result.c_str(), result.size(), 0, (struct sockaddr*)&peer, len);
                /* if(send_ret < 0) {
                    LOG(LogLevel::ERROR) << "sendto failed, errno: " << errno;
                } else {
                    LOG(LogLevel::DEBUG) << "reply sent: " << echo_string;
                } */
            }
        }
    }

    // 析构函数
    ~UdpServer()
    {
    }

private:
    int _sockfd; // 套接字描述符
    uint16_t _port; // 端口号
    /* std::string _ip; */ //字符串型IP地址, "192.168.1.1"
    bool _isrunning; // 是否运行中

    func_t _func; // 回调函数
};